// index page
// ============

define(['require', 'zepto', 'wx', 'utils', 'qiniu', 'qiniuPlayer', 'sui'], function(require, $, wx, utils, qiniu, QPlayer) {
      return {
        init : function(data) {
          console.log(window.__CTX__);
          this.phase = data.phase;
          this.status = data.status;
          this.betId = data.betId;
          this.player;
          this.bindUpload(data.jsConfig)
          this.submitVideo();
        },
        
        bindUpload: function(config) {
          var that = this;
          
          var currentVideo = $("#uploadVideo").data("video");
          if(currentVideo) {
            that.player = new QPlayer({
              url: currentVideo + '?avvod/m3u8/s/960x640/vb/1000k',
              container: document.getElementById("uploadVideo"),
              defaultViewConfig: {
                showControls: true,
              }
            });
          }
          
          $(document).on("change", "input[name='selectVideo']", function(e){
            
            $.modal({
              title:  '开始上传',
              text: '<p class="processing">进度：0%</p>',
              buttons: []
            })
            var $input = this;
            
            $.ajax({
              
              url: window.__CTX__.cloudToken,
              // The name of the callback parameter, as specified by the YQL service
              jsonp: "callback",
              // Tell jQuery we're expecting JSONP
              dataType: "jsonp",
              // Work with the response
              success: function( response ) {
                var token = response.token;
                var name = $input.files[0].name;
                var nameSplit = name.split(".");
                var surfix = nameSplit[nameSplit.length -1];
                var key = new Date().getTime() + "." + surfix;
                that.uploadKey = key;
                var putExtra = {
                        fname: name,
                        params: {},
                        mimeType: null
                      };
                
                var config = {
                        useCdnDomain: true,
                        disableStatisticsReport: false,
                        retryCount: 6,
                        region: null
                      };
                var observable = qiniu.upload($input.files[0], key, token, putExtra, config);
                observable.subscribe(function(response){
                  let total = response.total;
                  var processing = parseInt(total.percent)
                  $(".processing").html("进度：" + processing + "% ");
                  if(processing == 100) {
                    $.closeModal();
                    if(that.player) that.player.destroy();
                    $("#uploadVideo").find("img").addClass("hide");
                    that.player = new QPlayer({
                      url: window.__CTX__.cloudHost + "/" + key,
                      container: document.getElementById("uploadVideo"),
                      defaultViewConfig: {
                        showControls: true,
                      }
                    });
                  }
                });
              },
              fail: function(e) {
                $.taost("选择失败");
                window.location.reload();
              }
            });
            
          })
       },
       
       submitVideo: function() {
         var that = this;
         $(document).on('click', '#uploadVedio', function(e) {
           e.preventDefault();
           
           if(!that.uploadKey) {
             $.toast("请选择视频上传");
             return;
           }
           
           $.showPreloader('正在保存')
           $.post(window.__CTX__.root + '/profile/bets/' + that.betId + '/vedio-upload?phase=' + that.phase + "&status="+ that.status + "&videoPath=" + that.uploadKey)
            .done(function(response) {
              $.hidePreloader();
              utils.processResult(response, function(data) {
                window.location.href = window.__CTX__.root + '/profile/bets/' + that.betId + "/" + that.phase;
              }, function() {
                $.toast(result.message || '保存成功')
              })
            })
         })
       }
    }
    })